package com.neusoft.neuiotms.dcam.fixed.service.spareconsume.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.bizcore.activiti.service.ProcessInstanceService;
import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.constant.DCAMConsts;
import com.neusoft.neuiotms.dcam.fixed.converter.spareconsume.SpareConsumeBackConver;
import com.neusoft.neuiotms.dcam.fixed.dto.spareconsume.SpareConsumeBackDTO;
import com.neusoft.neuiotms.dcam.fixed.model.spareconsume.SpareConsumeApply;
import com.neusoft.neuiotms.dcam.fixed.model.spareconsume.SpareConsumeBack;
import com.neusoft.neuiotms.dcam.fixed.model.spareconsume.SpareConsumeStock;
import com.neusoft.neuiotms.dcam.fixed.repository.spareConsume.SpareConsumeApplyRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.spareConsume.SpareConsumeBackRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.spareConsume.SpareConsumeStockRepository;
import com.neusoft.neuiotms.dcam.fixed.service.spareconsume.SpareConsumeBackService;

@Service
public class SpareConsumeBackServiceImpl implements SpareConsumeBackService {

    @Autowired
    private ProcessInstanceService processInstanceService;

    @Autowired
    private SpareConsumeBackRepository spareConsumeBackRepository;

    @Autowired
    private SpareConsumeBackConver spareConsumeBackConver;

    @Autowired
    private SpareConsumeApplyRepository spareConsumeApplyRepository;

    @Autowired
    private SpareConsumeStockRepository spareConsumeStockRepository;

    @Transactional
    @Override
    public List<SpareConsumeBackDTO> create(final List<SpareConsumeBackDTO> dtos, final UserBean user) {
        // 完成节点
        if ((dtos != null) && (dtos.size() > 0)) {
            final SpareConsumeBackDTO dto = dtos.get(0);
            this.processInstanceService.complete(dto.getWorkOrderNum(), dto.getTaskId(), user);
            final List<SpareConsumeBack> lstStocks =
                    this.spareConsumeBackRepository.findByWorkNumAndTaskId(dto.getWorkOrderNum(),
                            dto.getTaskId());
            this.spareConsumeBackRepository.deleteInBatch(lstStocks);

            // 修改备品备件状态
            final List<SpareConsumeStock> stocks =
                    this.spareConsumeStockRepository.findByWorkNum(dto.getWorkOrderNum());
            if (stocks != null) {
                stocks.forEach(r -> {
                    if (r.getStock() != null) {
                        r.getStock().setStatus(DCAMConsts.STOCK_STATUS_USED);
                        r.getStock().setUpdateTime(new Date());
                    }
                });
            }
            this.spareConsumeStockRepository.saveAll(stocks);

            // 新增备品备件的领用人
            final SpareConsumeApply spareConsumeApply =
                    this.spareConsumeApplyRepository.findTopByWorkNumOrderByIdDesc(dto.getWorkOrderNum());
            spareConsumeApply.setConfirmUser(dto.getRequirePerson());
            this.spareConsumeApplyRepository.save(spareConsumeApply);
            lstStocks.clear();
            dtos.forEach(r -> {
                if (!"new".equals(r.getStatus())) {
                    lstStocks.add(this.spareConsumeBackConver.toModel(r));
                }
            });
            if (lstStocks.size() > 0) {
                this.spareConsumeBackRepository.saveAll(lstStocks);
                dtos.clear();
                lstStocks.forEach(r -> {
                    dtos.add(this.spareConsumeBackConver.toDTO(r));
                });
            }
        }
        return dtos;
    }

    @Override
    public List<SpareConsumeBackDTO> detail(final String workNum, final String taskId, final UserBean user) {
        return null;
    }

    @Override
    public Page<SpareConsumeBackDTO> search(final Pageable pageable, final Searchable searchable) {
        final Page<SpareConsumeBack> result = this.spareConsumeBackRepository.search(pageable, searchable);
        String confirmUser = "";
        if (StringUtils.isNotEmpty(searchable.getStrValue("workOrderNum"))) {
            final SpareConsumeApply spareConsumeApply =
                    this.spareConsumeApplyRepository.findTopByWorkNumOrderByIdDesc(searchable.getStrValue("workOrderNum"));
            confirmUser = spareConsumeApply.getConfirmUser();
        }
        final Page<SpareConsumeBackDTO> res = this.spareConsumeBackConver.toPageDTO(result);
        if ((res != null) && (res.getContent() != null) && (res.getContent().size() > 0)) {
            final String finalConfirmUser = confirmUser;
            res.getContent().forEach(r -> {
                r.setConfirmUser(finalConfirmUser);
            });
        } else {
            //            SpareConsumeBackDTO spareConsumeBackDTO = new SpareConsumeBackDTO();
            //            spareConsumeBackDTO.setConfirmUser(confirmUser);
            //            if(res.getContent() != null){
            //                res.getContent().add(spareConsumeBackDTO);
            //            }
        }
        return res;
    }
}
